package exam.controller;


import exam.bean.PaperInfo;
import exam.bean.Student;
import exam.service.IStudentService;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@RestController()
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private IStudentService studentService;

    @PreAuthorize("hasAuthority('sys:student:list')")
    @ResponseBody
    @GetMapping("/student")
    public Msg Student(
            @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
            @RequestParam(value = "uName",defaultValue = "") String uName
    ){
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("name",uName);
        return  Msg.success().add("pageInfo",studentService.selectInfo(pageNum,pageSize,queryMap));
    }

    @PreAuthorize("hasAuthority('sys:student:detail')")
    @ResponseBody
    @GetMapping("/detail")
    public Msg detail(@RequestParam(value="id") int id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return Msg.success().add("studentInfo",studentService.selectInfoSingle(map));
    }

    @PreAuthorize("hasAuthority('sys:student:delete')")
    @ResponseBody
    @GetMapping("/delete")
    public Msg delete (@RequestParam(value="id") int id){
        boolean re =  studentService.deleteInfo(id) > 0 ;
        return Msg.success().setMessage( re ? "删除成功" : "删除失败");
    }

    @PreAuthorize("hasAuthority('sys:student:add')")
    @PostMapping("/add")
    public Msg add(@RequestBody Student student){
        boolean re = studentService.insertInfo(student) > 0 ;
        return Msg.success().setMessage(re ? "添加成功" : "添加失败");
    }

    @PreAuthorize("hasAuthority('sys:student:update')")
    @ResponseBody
    @PostMapping("/upData")
    public Msg update(@RequestBody Student student){
        boolean re = studentService.updateInfo(student) > 0;
        return Msg.success().setMessage(re ? "更新成功" : "更新失败");
    }


}
